package com.example.springbootlearn.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.MultiValueMap;

import java.util.Objects;

/**
 * @author caiji Mr. Li
 * @date 2019/10/19 16:25
 */
public class OnSystemPropertyConditional implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(ConditionalOnSystemProperty.class.getName());
        String name = (String) attributes.getFirst("name");
        String value = (String) attributes.getFirst("value");

        String systemPropertyValue = System.getProperty(name);
        if (Objects.equals(value, systemPropertyValue)) {
            System.out.println("-------系统属性找到匹配-------" + value);
            return true;
        }
        return false;
    }
}
